package roleEditor.view
{
	import asset.roleEditor.previewViewAsset;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	
	import helper.LoaderHelper;
	import helper.URLLoaderHelper;
	
	import role.RoleCharacterCreatePlayerBase;
	import role.RoleCharacterStateItem;
	import role.RoleCharacterStateSet;
	
	import roleEditor.model.RoleModel;
	import roleEditor.vo.RoleVO;
	
	public class PreviewView extends previewViewAsset
	{
		private var _model:RoleModel;
		private var _urlLoader:URLLoaderHelper;
		private var _xmlRoleConfig:XML;
		private var _xmlStates:XMLList;
		private var _previewListBox:Sprite;
		private var _assetItemComplete:int=0;
		private var _roleCharacterCreatePlayerBase:RoleCharacterCreatePlayerBase;
		
		private var _roleCharacterStateSet:RoleCharacterStateSet=new RoleCharacterStateSet();
		
		public function PreviewView(model:RoleModel)
		{
			_model=model;
			setView();
			setEvent();
		}
		
		private function setEvent():void
		{
			
		}
		
		private function setView():void
		{
			if(previewListPos.parent) previewListPos.parent.removeChild(previewListPos);
			_previewListBox=new Sprite();
			_previewListBox.x=previewListPos.x;
			_previewListBox.y=previewListPos.y;
			addChild(_previewListBox);
			
			_urlLoader=new URLLoaderHelper("role/role1.xml", 0, onRoleConfigCallBack);
			_urlLoader.loadSync();
		}
		
		private function onRoleConfigCallBack(data:*):void
		{
			_xmlRoleConfig=new XML(String(data));
			
			
			_roleCharacterCreatePlayerBase=new RoleCharacterCreatePlayerBase(_xmlRoleConfig, roleCallBack);
			
			_xmlStates=_xmlRoleConfig.states.state;//取得状态集
			for each(var state:XML in _xmlStates)
			{
				if(state.actions.action.length()>0)
				{
					var num:int=0;
					for each(var item:XML in _xmlStates.actions.action)
					{//动作集处理
						var itemView:PreviewListItemView=new PreviewListItemView();
						itemView.buttonMode=true;
						itemView.actionName=item.@name;
						itemView.actionType=item.@type;
						itemView.stateType=state.@type;
						itemView.y=itemView.height*num+5;
						itemView.addEventListener(MouseEvent.CLICK, onListItemClick);
						_previewListBox.addChild(itemView);
						num++;
					}
				}
			}
		}
		
		private function onListItemClick(evt:MouseEvent):void
		{
			var item:PreviewListItemView=evt.currentTarget as PreviewListItemView;
			
			_roleCharacterCreatePlayerBase.sceneCharacterStateType=item.stateType;//切换状态
			_roleCharacterCreatePlayerBase.sceneCharacterActionType=item.actionType;//切换动作
		}
		
		private function roleCallBack(roleCharacterCreatePlayerBase:RoleCharacterCreatePlayerBase, isLoadSucceed:Boolean):void
		{
			roleCharacterCreatePlayerBase.x=(previewBox.width-roleCharacterCreatePlayerBase.width)/2;
			roleCharacterCreatePlayerBase.y=(previewBox.height-roleCharacterCreatePlayerBase.height)/2;
			previewBox.addChild(roleCharacterCreatePlayerBase);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		/**
		 * 重绘所有玩家
		 */		
		private function onEnterFrame(evt:Event):void
		{
			_roleCharacterCreatePlayerBase.update();
		}
		
		public function dispose():void
		{
			if(_urlLoader) _urlLoader.dispose();
			_urlLoader=null;	
		}
	}
}